package com.itheima.reggie.controller;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.net.ssl.HandshakeCompletedEvent;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/login")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    //员工登录接口
    @PostMapping("/employee/login")
    public R<Employee> login(Employee employee){
        log.info("员工登录,{}用户名：{}", employee.getUsername());
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        queryWrapper.eq(Employee::getPassword, DigestUtils.md5Digest(employee.getPassword().getBytes()));
        Employee emp = employeeService.getOne(queryWrapper);
        if(emp ==null){
            log.error("输入的用户名和密码错误，employee对象:{}", JSON.toJSONString(employee));
            return R.error("您输入的用户名或者密码错误,请重新输入");
        }
        if (0 == emp.getStatus()){
            log.error("当前员工:{} 已被禁用,无法登录",emp.getUsername());
            return R.error("您的账号已被禁用,请联系管理员");
        }
        return  R.success(emp);
    }

    //员工信息分页查询接口
    public R<Page> page(int page, int pageSize, String name){
       log.info("page={}, pageSize={}, name={}", page, pageSize, name);
       Page<Employee> pageInfo = new Page<>(page,pageSize);
       LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
       queryWrapper.like(Employee::getName,name);
       queryWrapper.orderByDesc(Employee::getUpdateTime);
       employeeService.page(pageInfo,queryWrapper);
       return R.success(pageInfo);
    }

    //根据id查询员工信息接口
    @GetMapping({"/{id}"})
    public R<Employee> getByID(@PathVariable Long id){
       log.info("根据id查询员工信息...");
       Employee employee = employeeService.getById(id);
       if (employee!=null){
           return R.success(employee);
       }
        return R.error("没有查询到对应员工信息");
    }

    //根据id修改员工信息接口
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){
        log.info(employee.toString());
        long id = Thread.currentThread().getId();
        log.info("线程id为:{}",id);
        Long empId = (Long) request.getSession().getAttribute("employee");
        employee.setUpdateUser(empId);
        employee.setUpdateTime(LocalDateTime.now());
        employeeService.updateById(employee);
        return R.success("员工信息修改成功");
    }

    //退出登录接口
   @PostMapping("/logout")
   public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    //新增员工信息接口
    @PostMapping
    public R<String> save(HttpServletRequest request,@RequestBody Employee employee){
        log.info("新增员工，员工信息：{}", employee.toString());
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        Long empId = (Long) request.getSession().getAttribute("employee");
        employee.setCreateUser(empId);
        employee.setUpdateUser(empId);
        employeeService.save(employee);
        return R.success("新增员工成功");
    }
}
